//
//  CZTaskCenterViewController.m
//  nsj_iOS
//
//  Created by apple on 2017/1/9.
//  Copyright © 2017年 GCZ. All rights reserved.
//

#import "CZTaskCenterViewController.h"
#import "CZTaskListViewController.h"
#import "CZMyTaskViewController.h"
#import "CZTaskDetailsViewController.h"
#import "CZVideoPlayViewController.h"
@interface CZTaskCenterViewController () <CAAnimationDelegate, UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *taskListBtn;
@property (weak, nonatomic) IBOutlet UIButton *myTastBtn;
@property (weak, nonatomic) IBOutlet UIView *yellowView;
@property (weak, nonatomic) IBOutlet UIView *contentView;

@property (nonatomic, strong) CZTaskListViewController *taskListVC;
@property (nonatomic, strong) CZMyTaskViewController *myTaskVC;

@end

@implementation CZTaskCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [(UIView*)[self.navigationController.navigationBar.subviews objectAtIndex:0] setAlpha:1.f];
    self.navigationController.delegate = self;

    CZTaskListViewController *taskListVC = [[CZTaskListViewController alloc] init];
    _taskListVC = taskListVC;
    CZMyTaskViewController *myTaskVC = [[CZMyTaskViewController alloc] init];
    _myTaskVC = myTaskVC;
    
    [taskListVC getTaskListData];
    
    [self addChildViewController:taskListVC];
    [self addChildViewController:myTaskVC];
    
    _taskListVC.view.frame = self.contentView.bounds;
    [self.contentView addSubview:_taskListVC.view];
    // Do any additional setup after loading the view from its nib.
}
- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
    [super viewWillAppear:animated];
}
- (IBAction)taskListAction:(UIButton *)sender {
    if (sender.selected) {
        return;
    }
    sender.selected = YES;
    _myTastBtn.selected = NO;
    if (self.contentView.subviews.count > 0) {
        [self.contentView.subviews[0] removeFromSuperview];
    }
    sender.highlighted = NO;
    _myTastBtn.highlighted = YES;
    _taskListVC.view.frame = self.contentView.bounds;
    [self.contentView addSubview:_taskListVC.view];
    [self setAnimationWithKey:@"positionLeft"];
}

- (IBAction)myTaskAction:(UIButton *)sender {
    if (sender.selected) {
        return;
    }
    if (self.contentView.subviews.count > 0) {
        [self.contentView.subviews[0] removeFromSuperview];
    }

    sender.selected = YES;
    _taskListBtn.selected = NO;
    sender.highlighted = NO;
    _taskListBtn.highlighted = YES;
    _myTaskVC.view.frame = self.contentView.bounds;
    [self.contentView addSubview:_myTaskVC.view];

    [self setAnimationWithKey:@"positionRight"];

}

- (void)setAnimationWithKey:(NSString *)key {
    [_yellowView.layer removeAllAnimations];
    CGPoint position = _yellowView.layer.position;
    CABasicAnimation *basic1 = [CABasicAnimation animationWithKeyPath:@"position.x"];
    
    if ([key isEqualToString:@"positionRight"]) {
        basic1.toValue = [NSNumber numberWithFloat:(position.x + Main_Screen_Width / 2)];
        basic1.fromValue = [NSNumber numberWithFloat:position.x];
    } else {
        basic1.fromValue = [NSNumber numberWithFloat:(position.x + Main_Screen_Width / 2)];
        basic1.toValue = [NSNumber numberWithFloat:position.x];
    }
    
    basic1.duration = 0.2;
    basic1.fillMode=kCAFillModeForwards;
    basic1.removedOnCompletion = NO;
    [_yellowView.layer addAnimation:basic1 forKey:key];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
//    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    if ([viewController isKindOfClass:[self class]] || [viewController isKindOfClass:[CZVideoPlayViewController class]]) {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    } else {
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
